<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Auction
 * @version 	: 1.1
 * COPYRIGHT    : 2010
 * ------------------------------------------------------
 *
 * Created on	:  Sep 13, 2010
 *
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls('controllers.AbstractBackEndController');
session_start();
RCoreImporter::cls( "mvc.AbstractController" );
RClassesImporter::cls('dbo.dtos.Member_eventDTO');
// Import class to validator
RPluginImporter::cls('authenticate.FrontAuthenticateFinder');
RPluginImporter::cls('authenticate.BackAuthenticateFinder');
/**
 * The controller for State application
 *
 */
class EventFrontController extends AbstractBackEndController {

	var $_model;

	function __construct() {
		parent::__construct();
		SysApplication::importModelCls('EventModel');
		$this->_model = new EventModel();
		$this->setRedirect($url);


	}

	function display() {
		$root = SYS_ROOT_URL;
		$model = $this->_model;
		$lang = $_REQUEST['lang'];
		$list_event = SysLanguage::_('LIST_EVENT');
		//-----------Load event---------------
		$sql1 = "SELECT * FROM event ORDER BY id DESC  LIMIT 3";
		$result_event = SysDatabase::loadObjectList($sql1);
		$event_item = "";
		foreach ($result_event as $row){
			$id = $row->id;
			$start = $row->start_date;
			$end = $row->end_date;
			$date  = date("d-m-Y");
			if (strtotime($date) > strtotime($start) && strtotime($date) < strtotime($end) ){
				if($_REQUEST['lang'] == 'fr'){
					$btnEvent = 'Participer';
						
				} else if($_REQUEST['lang'] == 'en'){
					$btnEvent = 'Join it';
						
				}
			}else {
				$btnEvent = "";
			}

			if($_REQUEST['lang'] == 'fr'){
				$title1 = $row->title_fr;
			} else if($_REQUEST['lang'] == 'en'){
				$title1 = $row->title_en;
			}
			$thumb = $row->thumbs;
			eval('$event_item .= "'.SysApplication::loadTemplate('front/event_item').'";');
		}





		$news = SysLanguage::_("NEWS");
		$newsDesc  = SysLanguage::_("NEWSDESC");
		$viewall = SysLanguage::_('VIEWALL');
		$limit = 12;
		$ypage = SysRequest::getVar('vpage', 1);
		$events = $model->getEventPaging($ypage, $limit);
		$listEvent = $events->getRecordSet();
		$itemEvent = "";


		//$listEvent = $model->loadListEvent();
		//$itemEvent = "";
		foreach ($listEvent as $item){
			$id = $item->id;
			$start = $item->start_date;
			$end = $item->end_date;
			$date  = date("d-m-Y");
			if (strtotime($date) > strtotime($start) && strtotime($date) < strtotime($end) ){
			if($_REQUEST['lang'] == 'fr'){
					$btnEvent = 'Participer';
					
				} else if($_REQUEST['lang'] == 'en'){
					$btnEvent = 'join it';
					
				}
			}else {
				$btnEvent = "";
			}
			if($_REQUEST['lang'] == 'fr'){
				$des = $item->title_fr;
			} else if($_REQUEST['lang'] == 'en'){
				$des = $item->title_en;
			}
			$thumb = $item->thumbs;
			eval('$itemEvent .= "'.SysApplication::loadTemplate('front/itemEvent').'";');

		}


		$pagerVideo = SysTemplate::getPaginationInstance($events->getTotalRecords(), $limit, "?vpage");
		$pagerVideo->setPagerText('<<','<','>','>>');
		$pagingVideoHtml = $pagerVideo->renderPager($ypage);
		eval('echo "'.SysApplication::loadTemplate('front/default').'";');
	}
	function eventDetail() {
		$root = SYS_ROOT_URL;
		$lang = $_REQUEST['lang'];
		$viewall = SysLanguage::_('VIEWALL');
		$news = SysLanguage::_("NEWS");
		$newsDesc  = SysLanguage::_("NEWSDESC");
		$join_event = SysLanguage::_('JOIN_EVENT');

	if($_REQUEST['lang'] == 'fr'){
					$btnEvent = 'Participer';
					
				} else if($_REQUEST['lang'] == 'en'){
					$btnEvent = 'Join it';
					
				}
		$model = $this->_model;
		//-----------Load event---------------
		$sql1 = "SELECT * FROM event ORDER BY id DESC  LIMIT 3";
		$result_event = SysDatabase::loadObjectList($sql1);
		$event_item = "";
		foreach ($result_event as $row){
			$id = $row->id;
			$start = $row->start_date;
			$end = $row->end_date;
			$date  = date("d-m-Y");
			if (strtotime($date) > strtotime($start) && strtotime($date) < strtotime($end) ){
			if($_REQUEST['lang'] == 'fr'){
					$btnEvent = 'Participer';
					
				} else if($_REQUEST['lang'] == 'en'){
					$btnEvent = 'Join it';
					
				}
			}else {
				$btnEvent = "";
			}
			if($_REQUEST['lang'] == 'fr'){
				$title1 = $row->title_fr;
			} else if($_REQUEST['lang'] == 'en'){
				$title1 = $row->title_en;
			}
			$thumb = $row->thumbs;
			eval('$event_item .= "'.SysApplication::loadTemplate('front/event_item').'";');
		}
		$id = SysRequest::getVar('task');
		$result = $model->loadEventDetails($id);

		if($_REQUEST['lang'] == 'fr'){
			$title = $result->title_fr;
			$des = $result->details_fr;
		} else if($_REQUEST['lang'] == 'en'){
			$title = $result->title_en;
			$des = $result->details_en;
		}

		eval('echo "'.SysApplication::loadTemplate('front/details').'";');
	}

	function joinIt() {
		$root = SYS_ROOT_URL;

		$viewall = SysLanguage::_('VIEWALL');
		$news = SysLanguage::_("NEWS");
		$newsDesc  = SysLanguage::_("NEWSDESC");

		$name = $_SESSION['username'];

		$model = $this->_model;
		$cid = $_SESSION['cid'];
		$username = $_SESSION['username'];
		//get User infomation
		$sqlUser = "SELECT * FROM member WHERE username = '$username'";
		$resultUser = SysDatabase::loadObject($sqlUser);

		$mName = $resultUser->username;
		$mBirthday = $resultUser->birthday;
		$mPhone = $resultUser->mobile;
		$mEmail = $resultUser->email;
		$mAddress = $resultUser->address;
		$mCompany = $resultUser->company;

		$memberEventDTO = new Member_eventDTO();
		$memberEventDTO->event_id = $cid;
		$memberEventDTO->name = $mName;
		$memberEventDTO->birthday = $mBirthday;
		$memberEventDTO->phone = $mPhone;
		$memberEventDTO->address = $mAddress;
		$memberEventDTO->company = $mCompany;

		//So sanh user va event_id
		$sql = "SELECT * FROM member_event";
		$resultMem = SysDatabase::loadObjectList($sql);
		foreach ($resultMem as $item){
			if ($memberEventDTO->name == $item->name && $memberEventDTO->event_id == $item->event_id){
				return $this->setRedirect($root.'event/joined');
			}
		}
		if ($resultUser != NULL){
			$result = $model->saveMemberDTO($memberEventDTO);
			$memberEventDTO->resetToNull();
		}



		//Load description
		$sqlDes = "SELECT * FROM settings WHERE id = 9	";
		$resultDes = SysDatabase::loadObject($sqlDes);
		if($_REQUEST['lang'] == 'fr'){
			$des = $resultDes->content_fr;
		} else if($_REQUEST['lang'] == 'en'){
			$des = $resultDes->content_en;
		}

		//-----------Load event---------------
		$sql1 = "SELECT * FROM event ORDER BY id DESC  LIMIT 3";
		$result_event = SysDatabase::loadObjectList($sql1);
		$event_item = "";
		foreach ($result_event as $row){
			$id = $row->id;
			$start = $row->start_date;
			$end = $row->end_date;
			$date  = date("d-m-Y");
			if (strtotime($date) > strtotime($start) && strtotime($date) < strtotime($end) ){
			if($_REQUEST['lang'] == 'fr'){
					$btnEvent = 'Participer';
					
				} else if($_REQUEST['lang'] == 'en'){
					$btnEvent = 'Join it';
					
				}
			}else {
				$btnEvent = "";
			}
			if($_REQUEST['lang'] == 'fr'){
				$title1 = $row->title_fr;
			} else if($_REQUEST['lang'] == 'en'){
				$title1 = $row->title_en;
			}
			$thumb = $row->thumbs;
			eval('$event_item .= "'.SysApplication::loadTemplate('front/event_item').'";');
		}

		eval('echo "'.SysApplication::loadTemplate('front/join_it').'";');
	}
	function joined() {
		$root = SYS_ROOT_URL;
		$viewall = SysLanguage::_('VIEWALL');
		$news = SysLanguage::_("NEWS");
		$newsDesc  = SysLanguage::_("NEWSDESC");
		//$this->setRedirect($root.'event/joined');
		//Load description
		$sqlDes = "SELECT * FROM settings WHERE id = 10	";
		$resultDes = SysDatabase::loadObject($sqlDes);
		if($_REQUEST['lang'] == 'fr'){
			$des = $resultDes->content_fr;
		} else if($_REQUEST['lang'] == 'en'){
			$des = $resultDes->content_en;
		}
		$name = $_SESSION['username'];

		//-----------Load event---------------
		$sql1 = "SELECT * FROM event ORDER BY id DESC  LIMIT 3";
		$result_event = SysDatabase::loadObjectList($sql1);
		$event_item = "";
		foreach ($result_event as $row){
			$id = $row->id;
			$start = $row->start_date;
			$end = $row->end_date;
			$date  = date("d-m-Y");
			if (strtotime($date) > strtotime($start) && strtotime($date) < strtotime($end) ){
			if($_REQUEST['lang'] == 'fr'){
					$btnEvent = 'Participer';
					
				} else if($_REQUEST['lang'] == 'en'){
					$btnEvent = 'Join it';
					
				}
			}else {
				$btnEvent = "";
			}
			if($_REQUEST['lang'] == 'fr'){
				$title1 = $row->title_fr;
			} else if($_REQUEST['lang'] == 'en'){
				$title1 = $row->title_en;
			}
			$thumb = $row->thumbs;
			eval('$event_item .= "'.SysApplication::loadTemplate('front/event_item').'";');
		}

		//Load title------------
		$sql = "SELECT * FROM title WHERE id=9";
		$resultTitle = SysDatabase::loadObject($sql);
		if($_REQUEST['lang'] == 'fr'){
			$title = $resultTitle->title_fr;
		} else if($_REQUEST['lang'] == 'en'){
			$title = $resultTitle->title_en;
		}

		eval('echo "'.SysApplication::loadTemplate('front/joined').'";');
	}
	function subcription(){
		$subcEvent = SysLanguage::_("EVENT_SUBC");
		$enter_pass = SysLanguage::_('ENTER_PASS');
		$loginf = SysLanguage::_('LOGINF');
		$register = SysLanguage::_('REGISTER');
		$forgot = SysLanguage::_('FORGOT');
		$subcf = SysLanguage::_('SUBCF');
		$sdesc = SysLanguage::_('SDESC');
		$fullname = SysLanguage::_('FULLNAME');
		$birthday = SysLanguage::_('BIRTHDAY');
		$phone = SysLanguage::_('PHONE');
		$email = SysLanguage::_('EMAIL');
		$company = SysLanguage::_('COMPANY');
		$address = SysLanguage::_('ADDRESS');
		$news = SysLanguage::_("NEWS");
		$newsDesc  = SysLanguage::_("NEWSDESC");
		$join_event = SysLanguage::_('JOIN_EVENT');
		$username = SysLanguage::_('USERNAME');
		$password = SysLanguage::_('PASSWORD');
		if($_REQUEST['lang'] == 'fr'){
			$btnEvent = "templates/kbbv/images/eventfr.png";
		} else if($_REQUEST['lang'] == 'en'){
			$btnEvent = "templates/kbbv/images/event.png";

		}




		$root = SYS_ROOT_URL;
		$cid = SysRequest::getVar('task');
		$model = $this->_model;
		$_SESSION['cid'] = $cid;
		if ($_SESSION['_RU_AuthenticateObjfront']->username){
			$this->setRedirect($root.'event/joinIt');
		}
		$susername = $_SESSION['username'];
		$sbirthday = $_SESSION['birthday'];
		$sphone = $_SESSION['phone'];
		$semail = $_SESSION['email'];
		$saddress = $_SESSION['address'];
		$scompany = $_SESSION['company'];

		if ($_POST['subcript']){
			$name = $_POST['name'];
			$birthday = $_POST['birthday'];
			$phone = $_POST['phone'];
			$email = $_POST['email'];
			$address = $_POST['address'];
			$company = $_POST['company'];

			$_SESSION['username'] = $name;
			$_SESSION['birthday'] = $birthday;
			$_SESSION['phone'] = $phone;
			$_SESSION['email'] = $email;
			$_SESSION['address'] = $address;
			$_SESSION['company'] = $company;


			if ($name == "" || $birthday = "" || $phone == "" || $email = "" || $address = ""){
				$warning = "Fields (*) is can not be null!";
			}else {

				$memberEventDTO = new Member_eventDTO();
				$memberEventDTO->bind($_POST);
				$memberEventDTO->event_id = $cid;
				if ($memberEventDTO->checkExisting("event_id", true) && $memberEventDTO->checkExisting("email", true)){
					$this->setRedirect($root.'event/joined');
				}else {
					$result = $model->saveMemberDTO($memberEventDTO);
					$memberEventDTO->resetToNull();
					$this->setRedirect($root.'event/joinIt');
				}

			}
		}


		//-----------Load event---------------
		$sql1 = "SELECT * FROM event ORDER BY id DESC  LIMIT 3";
		$result_event = SysDatabase::loadObjectList($sql1);
		$event_item = "";
		foreach ($result_event as $row){
			$id = $row->id;
			$start = $row->start_date;
			$end = $row->end_date;
			$date  = date("d-m-Y");
			if (strtotime($date) > strtotime($start) && strtotime($date) < strtotime($end) ){
			if($_REQUEST['lang'] == 'fr'){
					$btnEvent = 'Participer';
					
				} else if($_REQUEST['lang'] == 'en'){
					$btnEvent = 'Join it';
					
				}
			}else {
				$btnEvent = "";
			}
			if($_REQUEST['lang'] == 'fr'){
				$title1 = $row->title_fr;
			} else if($_REQUEST['lang'] == 'en'){
				$title1 = $row->title_en;
			}
			$thumb = $row->thumbs;
			eval('$event_item .= "'.SysApplication::loadTemplate('front/event_item').'";');
		}

		eval('echo "'.SysApplication::loadTemplate('front/subcription').'";');

	}
	/**
	 * Check for login
	 *
	 */
	function _checkLogin() {
		if (empty ( $_POST ['username'] ) && empty ( $_POST ['password'] )) {
			return "";
		}

		if (SystemSite::isBackEnd()) {
			$isCorrect = SysAuthenticate::backLogin($_POST ['username'], $_POST ['password']);
		}
		// TODO for front-end
		if(SystemSite::isFrontEnd()) {
			$isCorrect = SysAuthenticate::frontLogin($_POST ['username'], $_POST ['password']);

			$_SESSION['username'] = $_POST ['username'];

		}

		// you may check the access group here
		if (! $isCorrect) {
			$msg = SysLanguage::_( "WRONG_USER_PASS" );
			return $msg;
		}
		return true;
	}
}
